//
//  XKLEssenceViewController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/4/24.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLEssenceViewController.h"
#import "XKLAllController.h"


@interface XKLEssenceViewController()<UIScrollViewDelegate>
/**
 *  标题的view
 */
@property (nonatomic,weak)UIView *titleView;
/**
 *  指示当前选中标题的view
 */
@property (nonatomic,weak)UIView *HUDView;
/**
 *  当前选中的标题按钮
 */
@property (nonatomic,weak)UIButton *currentBut;
/**
 *  填充内容的scrollView
 */
@property (nonatomic,weak)UIScrollView *contentView;
@end


@implementation XKLEssenceViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor grayColor];
    //设置导航栏
    [self setNaviBar];
    
    //添加titleView
    [self upTitleView];
    //添加scrollView
    [self addContentView];
    
}
//获取是否是新帖
-(NSString *)topicType{
    return @"list";
}

//添加scrollView
-(void)addContentView{
    //不需要视同自动帮我们添加填充
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view insertSubview:scrollView atIndex:0];
    self.contentView=scrollView;
    scrollView.delegate=self;
    scrollView.bounces=NO;
    
    //根据有多少个按钮创建多少个控制器
    BOOL isOne=YES;
    for (UIButton *but in self.titleView.subviews) {
        if ([but isKindOfClass:[UIButton class]]) {
            XKLAllController *all = [[XKLAllController alloc] init];
            all.type = [self topicType];
            [self addChildViewController:all];
            
            
            if (isOne) {
               
                //先布局
                [self.view layoutIfNeeded];
                self.HUDView.width=but.titleLabel.width;
                self.HUDView.centerX=but.titleLabel.centerX;
                
                [self titleButClick:but];
                isOne=NO;
            }
        }
    }
    //设置contentSize
    scrollView.contentSize=CGSizeMake(ScreenW *self.childViewControllers.count, self.view.height);
    //开启分页
    scrollView.pagingEnabled=YES;
}

//设置导航栏
-(void)upTitleView{
    //创建一个view存放title按钮
    UIView *titleView=[[UIView alloc] init];
    [self.view addSubview:titleView];
    self.titleView=titleView;
    titleView.backgroundColor=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:5.0];
    titleView.frame=CGRectMake(0, 64, ScreenW, 35);
    
   
    //按钮的文字
    NSArray *titles=@[@"全部",@"视频",@"声音",@"图片",@"段子"];
    CGFloat butWidth=ScreenW / titles.count;
    
    for (int i=0; i<titles.count; i++) {
        UIButton *but=[[UIButton alloc] init];
        but.frame=CGRectMake((butWidth * i), 0, butWidth, titleView.height);
        [titleView addSubview:but];
        but.tag=i;
        [but setTitle:titles[i] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [but addTarget:self action:@selector(titleButClick:) forControlEvents:UIControlEventTouchUpInside];
       
        
    }
    
    //添加指示器
    UIView *HUDview=[[UIView alloc] init];
    HUDview.height=2;
    HUDview.y=titleView.height-2;
    [titleView addSubview:HUDview];
    self.HUDView=HUDview;
    HUDview.backgroundColor=[UIColor redColor];
   
    
}
//点击导航title
-(void)titleButClick:(UIButton *)but{
    self.currentBut.enabled=YES;
    but.enabled=NO;
    //指示器做动画
    [UIView animateWithDuration:0.25 animations:^{
        self.HUDView.width=but.titleLabel.width;
        self.HUDView.centerX=but.centerX;
    }];
    
    self.currentBut=but;
    [self switchTableView:but.tag];
}

//切换tableview
-(void)switchTableView:(NSInteger)index{
    static XKLAllController *childVC;
    childVC=self.childViewControllers[index];

    //判断子控件有没有父控件
    if (childVC.view.superview==nil) {
        //没有就添加
        [self.contentView addSubview:childVC.view];
        childVC.view.x=index*ScreenW;
        if (index==0) {
            childVC.topicType = kXKLTopicTypeAll;
        }else if (index == 1){
            childVC.topicType = kXKLTopicTypeVideo;
            
        }else if (index == 2){
            childVC.topicType = kXKLTopicTypeVoice;
        }else if (index == 3){
            childVC.topicType = kXKLTopicTypePicture;
        }else{
            childVC.topicType = kXKLTopicTypeWord;
        }
    }
    CGPoint conteneOffset=self.contentView.contentOffset;
    conteneOffset.x=ScreenW*index;
    self.contentView.contentOffset=conteneOffset;
}



//设置导航栏
-(void)setNaviBar{
    self.navigationItem.titleView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImageName:@"nav_item_game_iconN" seletImageName:@"nav_item_game_click_icon" target:self sel:@selector(gameClick:)];
    
    
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationButtonRandom" seletImageName:@"navigationButtonRandomClick" target:self sel:@selector(reandomClick:)];
}

-(void)gameClick:(UIButton *)but{
    UIViewController *VC=[[UIViewController alloc] init];
    VC.view.backgroundColor=[UIColor yellowColor];
    [self.navigationController pushViewController:VC animated:YES];
}
-(void)reandomClick:(UIButton *)but{
    XKLLog(@"%s",__func__);
}

#pragma mark - scrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //算出第几个
    NSUInteger index=scrollView.contentOffset.x/ScreenW;

    UIButton *but=(UIButton *)self.titleView.subviews[index];
    [self titleButClick:but];

}
//-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    XKLLogFunc;
//}
//-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//    NSUInteger index=scrollView.contentOffset.x/ScreenW;
//    
//    UIButton *but=(UIButton *)self.contentView.subviews[index];
//    [self titleButClick:but];
//}

@end
